define(['jquery', 'bootstrap', 'backend', 'table', 'form','util'], function ($, undefined, Backend, Table, Form,Util) {
    var Controller = {

        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xilujob.package_order/index' + location.search,
                    edit_url: 'xilujob.package_order/edit',
                    table: 'xilujob_package_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updatetime',
                search: false, //是否启用快速搜索
                commonSearch: true, //是否启用通用搜索
                searchFormVisible: true, //是否始终显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'platform', title: __('平台'), operate: false},
                        {field: 'order_no', title: __('订单号'), operate: 'LIKE'},
                        //{field: 'order_trade_no', title: __('Order_trade_no'), operate: 'LIKE'},
                        {field: 'pay_price', title: __('支付价格'), operate:false},
                        {field: 'pay_type', title: __('支付方式'), searchList: {"1":__('微信支付'),"2":__('支付宝'),"3":__('线下对公')}, formatter: Table.api.formatter.normal},
                        {field: 'pay_status', title: __('支付状态'), searchList: {"0":__('已取消'),"1":__('待支付'),"2":__('已支付')}, formatter: Table.api.formatter.status},
                        {field: 'paytime', title: __('支付时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'package_name', title: __('套餐名称'), operate: 'LIKE'},
                        {field: 'package_salesprice', title: __('套餐价格'), operate:false},
                        {field: 'createtime', title: __('创建时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('更新时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'),buttons:[
                                {
                                    name: 'edit',
                                    title: '支付',
                                    text: '支付',
                                    icon: '',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return $.fn.bootstrapTable.defaults.extend.edit_url+'?ids='+row.id
                                    },
                                    visible:function (row) {
                                        return row.pay_status == 1;
                                    }
                                },
                            ] ,table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        edit: function(){

            $(document).on('click',".pay-type input",function () {
                var payType = $(this).val();
                if(payType == 1){
                    $("#corporate").hide();
                }else{
                    $("#corporate").show();
                }
            })
            Form.api.bindevent($("form[role=form]"),function () {

                },
                function () {

                },
                function(success, error){
                    //注意如果我们需要阻止表单，可以在此使用return false;即可
                    //如果我们处理完成需要再次提交表单则可以使用submit提交,如下
                    var form = this;
                    if (form.size() === 0) {
                        Toastr.error("表单未初始化完成,无法提交");
                        return false;
                    }
                    var type = form.attr("method") ? form.attr("method").toUpperCase() : 'GET';
                    type = type && (type === 'GET' || type === 'POST') ? type : 'GET';
                    var url = form.attr("action");
                    url = url ? url : location.href;
                    Fast.api.ajax({
                        type: type,
                        url: url,
                        data: form.serialize(),
                        dataType: 'json',
                        complete: function (xhr) {
                            var result = xhr.responseJSON;
                            //关闭弹窗
                            if(result.code == 1){
                                //微信支付
                                if(result.data.pay_type == 1){
                                    Fast.api.ajax({
                                        type: "POST",
                                        url: Fast.api.fixurl("/api/xilujob.pay/pay"),
                                        data: {order_id:result.data.id,platform:result.data.platform,pay_type:result.data.pay_type},
                                        dataType: 'json',
                                        complete: function (xhr2){
                                            var result2 = xhr2.responseJSON;
                                            console.log(result2);
                                            if(result2.code == 1){
                                                if(result2.data.platform=='pc'){
                                                    var html = "<img style='display:block;margin-left:auto;margin-right:auto;' src="+result2.data.code_url+" />";
                                                    layer.open({
                                                        //formType: 2,//这里依然指定类型是多行文本框，但是在下面content中也可绑定多行文本框
                                                        title: '扫码付款',
                                                        area: ['400px', '400px'],
                                                        btnAlign: 'c',
                                                        closeBtn:'1',//右上角的关闭
                                                        content: html,
                                                        btn:['确认','关闭'],
                                                        yes: function (index, layero) {

                                                        }

                                                    });
                                                }
                                            }
                                        }
                                    });
                                }else{
                                    Toastr.success("订单更新成功，请尽快付款");
                                }
                            }else{
                                Toastr.error(result.msg);
                            }
                            return false;
                        }
                    }, function (data, ret) {
                        return false;
                    }, function (data, ret) {
                        return false;
                    });
                    return false;
                });
            //Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }

    };
    return Controller;
});